/* global window */
import queryString from 'query-string'
import modelExtend from 'dva-model-extend'
import { listall, query, create, update, remove, queryAll, patchremove } from 'services/permissions'
import { arrayToTree } from 'utils'
import { message } from 'antd'
import { pageModel } from './common'

export default modelExtend(pageModel, {
  namespace: 'permissions',

  state: {
    // currentItem: {},
    // modalVisible: false,
    // modalType: 'create',
    // selectedRowKeys: [],
    listAll: []
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/permissions') {
          const payload = queryString.parse(location.search) || { page: 1 }
          dispatch({
            type: 'list',
            payload,
          })
        }
      })
    },
  },

  effects: {
    * list ({ payload = {} }, { call, put }) {
      const data = yield call(listall, payload)
      const listData = data.data
      if (data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: arrayToTree(listData, 'id', 'bpid', 'children'),
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: listData.total,
            },
          },
        })
        yield put({ type: 'queryAll' })
      }
    },
    * queryAll ({}, { call, put }) {
      const { data, code } = yield call(queryAll)
      if(code === 0) {
        const _list = data.map(item => ({
          ...item,
          key: item.id,
          value: item.id,
          title: item.name,
        }))
        yield put({
          type: 'updateState',
          payload: {
            listAll: arrayToTree(_list, 'id', 'bpid', 'children'),
          }
        })
      }
    },
    * delete ({ payload }, { call, put, select }) {
      const data = yield call(remove, { id: payload })
      const { selectedRowKeys } = yield select(_ => _.permissions)
      if (data.success) {
        message.success('delete success')
        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
        yield put({ type: 'list' })
      } else {
        message.error('delete failed')
        throw data
      }
    },

    * multiDelete ({ payload }, { call, put }) {
      const data = yield call(patchremove, payload)
      if (data.success) {
        yield put({ type: 'updateState', payload: { selectedRowKeys: [] } })
        yield put({ type: 'list' })
      } else {
        throw data
      }
    },

    * create ({ payload }, { call, put }) {
      const data = yield call(create, payload)
      if (data.success) {
        message.success('create success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'list' })
      } else {
        message.error('create failed')
        throw data
      }
    },

    * update ({ payload }, { select, call, put }) {
      const id = yield select(({ permissions }) => permissions.currentItem.id)
      const newData = { ...payload, id }
      const data = yield call(update, newData)
      if (data.success) {
        message.success('update success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'list' })
      } else {
        message.error('update failed')
        throw data
      }
    },

    * showModal ({ payload }, { call, put }) {
      let sendData = {}
      sendData.id = payload.currentItem.id
      const data = yield call(query, sendData)
      let currData = data.data
      if (currData.mpid === -1){
        currData.visible = 1
      } else {
        currData.visible = 2
      }
      if (data.success) {
        yield put({
          type: 'updateModalState',
          payload: {
            modalType: 'update',
            currentItem: currData,
            modalVisible: true,
          },
        })
      } else {
        throw data
      }
    },

  },

  reducers: {

    // updateSelectState (state, { payload }) {
    //   return { ...state, ...payload }
    // },
    //
    // updateModalState (state, { payload }) {
    //   return { ...state, ...payload, modalVisible: true }
    // },
    //
    // hideModal (state) {
    //   return { ...state, modalVisible: false }
    // },
  },
})
